<?php
/*
 * @ControllerAnnotation(title="商品添加管理")
 */

namespace app\crm\controller\business;

use app\common\controller\CrmController;
use app\crm\model\BaseCourse;
use app\crm\traits\Curd;

class Product extends CrmController
{
    protected $validate = '';
    protected $model = 'BaseCourse';

    use Curd;

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * @NodeAnotation(title="列表")
     */    
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model::with('major,campus')
                ->where($where)
                ->count();
            $list = $this->model::with('major,campus')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 选择产品
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-17 17:54:58
     */
    public function change()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return $this->error('缺少id参数');
        }
        $rows = BaseCourse::where('id',$id)->field('name,sku,price')->find();
        if (empty($rows)) {
            return $this->error('无效的产品id');
        }
        $rows['course_id'] = $id;
        return $this->success('删除成功', '',$rows);
    }

}